package com.sgb.knowledgeshare.weight;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.widget.VideoView;
import com.swgk.core.util.ScreenUtil;


/**
 * description 全屏播放Videoview
 */
public class FullScreenVideoView extends VideoView {


    // x as width, y as height
    protected Point mVideoSize;
    public FullScreenVideoView(Context context) {
        super(context);
        mVideoSize = new Point(0,0);
    }

    public FullScreenVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mVideoSize = new Point(0,0);
    }

    public void setVideoSize(Point videoSize) {
        if (videoSize != null && !mVideoSize.equals(videoSize)) {
            this.mVideoSize = videoSize;
            requestLayout();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int videoWidth = mVideoSize.x;
        int videoHeight = mVideoSize.y;
        int width = getDefaultSize(videoWidth, widthMeasureSpec);
        int height = getDefaultSize(videoHeight, heightMeasureSpec);
        if (videoWidth > 0 && videoHeight > 0) {
            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

            // only the height is fixed, adjust the width to match aspect ratio if possible
            height = MeasureSpec.getSize(heightMeasureSpec);
            width = height * videoWidth / videoHeight;
            if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
                // couldn't match aspect ratio within the constraints
                width = widthSpecSize;
                height = width * videoHeight / videoWidth;
            }

            // only the width is fixed, adjust the height to match aspect ratio if possible
            width = widthSpecSize;
            height = width * videoHeight / videoWidth;
            if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
                // couldn't match aspect ratio within the constraints
                height = heightSpecSize;
                width = height * videoWidth / videoHeight;
            }
        }
        setMeasuredDimension(getDefaultSize(ScreenUtil.getDisplayWidth(), widthMeasureSpec), getDefaultSize(ScreenUtil.getDisplayHeight(), heightMeasureSpec));

    }
}
